package my_account;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Manage_addresses extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled8() throws Exception {
	/*driver.get(baseUrl);
    driver.findElement(By.linkText("My Account")).click();
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys("qatestingtestqa@gmail.com");
    driver.findElement(By.id("pass")).clear();
    driver.findElement(By.id("pass")).sendKeys("qwerty");
    driver.findElement(By.id("send2")).click();*/
    	driver.get(baseUrl + "/customer/account/");
    try {
      assertEquals("Manage Addresses", driver.findElement(By.linkText("Manage Addresses")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.linkText("Manage Addresses")).click();
    try {
      assertEquals("Address Book", driver.findElement(By.cssSelector("h1")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.cssSelector("div.page-title.title-buttons > button.button")).click();
    driver.findElement(By.id("telephone")).clear();
    driver.findElement(By.id("telephone")).sendKeys("87645312");
    driver.findElement(By.id("street_1")).clear();
    driver.findElement(By.id("street_1")).sendKeys("Tverskaya");
    driver.findElement(By.id("city")).clear();
    driver.findElement(By.id("city")).sendKeys("Volgograd");
    driver.findElement(By.id("zip")).clear();
    driver.findElement(By.id("zip")).sendKeys("8465123");
    new Select(driver.findElement(By.id("region_id"))).selectByVisibleText("Alabama");
    driver.findElement(By.cssSelector("div.buttons-set > button.button")).click();
    try {
      assertEquals("test test\n Tverskaya\n Volgograd, Alabama, 8465123\n United States\n T: 87645312", driver.findElement(By.cssSelector("div.col-2.addresses-additional > ol > li.item > address")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.linkText("Delete Address")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to delete this address[\\s\\S]$"));
    try {
      assertEquals("The address has been deleted.", driver.findElement(By.cssSelector("li > span")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertEquals("You have no additional address entries in your address book.", driver.findElement(By.cssSelector("li.item.empty > p")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
